package org.diy.traffic.sinks;

import org.diy.traffic.updates.TrafficUpdate;
import org.diy.traffic.updates.TrafficUpdate.UnknownDataException;

/**
 * This class is a stub representing the minimum amount of code required to create a custom TrafficSink. To be used for educational
 * purposes only, not really supposed to be instantiated.
 * @author Dan Greenblatt
 *
 */
public class TrafficSinkStub extends TrafficSink {

	public TrafficSinkStub(String sinkName, int rateLimit) {
		
		// Invoke the base class constructing, giving this instance of a TrafficSink a unique name
		// and also a max. number of updates per minute (optional).
		super("UNIQUE_NAME_FOR_THIS_INSTANCE", 20);

	}

	/**
	 * This function must be implemented; it is called by the OutputDaemon and should do all the heavy lifting
	 * required to send the supplied TrafficUpdate to the appropriate location.
	 */
	@Override
	public boolean processUpdate(TrafficUpdate update)
			throws TrafficSinkException {

		// Check to see if handling this update will put me over the pre-specified rate limit. The super-class will
		// check for me. 
		// TODO Ideally, this logic would be handled one level higher -- in the OutputDeamon so that when this function
		// is called, it can assume that it's OK to process the update
		if (isWithinRateLimit()) {
			
			// Get any extra info that would have been put in the object by the originating source
			try {
				Object o = update.getData("EXTRA_INFO");
			} catch (UnknownDataException e) {
				//oops, I guess this wasn't the type of TrafficUpdate I had thought, or
				// the originating TrafficSource forgot to supply required info
				return false;
			}
			
			// Do awesome stuff with this TrafficUpdate, like post it to your SpaceBook status!
			// 		mySpaceBook.updateStatus(update.getMessage());
			
			// if I do handle the update OK, then make sure to call this code in the base class, which does some accounting
			// to make sure it keeps track of how many updates I'm doing per minute.
			didHandleUpdate();
			return true;
		}
		return false;
	}

}
